//
//  BannerAdView.m
//  ebook
//
//  Created by 吴超飞 on 2016/11/23.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "BannerAdView.h"

@interface BannerAdView ()<BaiduMobAdViewDelegate>{//GADNativeExpressAdViewDelegate
    BannerAdLocation adlocation;
}

@property (nonatomic, retain) UIViewController *controller;
@property (nonatomic, retain) BaiduMobAdView *baiduAdView;
//@property (nonatomic, retain) GADNativeExpressAdView *nativeExpressAdView;

@property (nonatomic, strong) NSMutableArray *alterlist;

@end

@implementation BannerAdView

- (void)showAdInController:(UIViewController *)controller location:(BannerAdLocation)location{
    
    _controller = controller;
    adlocation = location;
    
    NSString *key = @"";
    switch (location) {
        case BannerAdInSearch:
            key = @"search_banner";
            break;
        default:
            break;
    }
    
    NSDictionary *dict = kBookCityDataDict;
    NSString *active = dict[@"ad"][key][@"active"];
    NSArray *alterlist = dict[@"ad"][key][@"alterlist"];
    _alterlist = [NSMutableArray arrayWithArray:alterlist];
    
    [self loadBannerAd:active];
}

//根据类型加载对应的广告
-(void)loadBannerAd:(NSString *)active{
    
    if([active isEqualToString:@"baidu"]){//显示百度广告
        [self loadBaiduAdView];
    }else if([active isEqualToString:@"google"]){
//        [self loadGoogleAdView];
    }
}

//一种广告失败后，加载另一种备选广告
-(void)loadNextBannerAd{
    
    if(_alterlist.count>0){
        NSString *next = _alterlist.firstObject;
        [self loadBannerAd:next];
        [_alterlist removeObjectAtIndex:0];
    }
}

- (void)loadBaiduAdView {
    
    [BaiduMobAdSetting setLpStyle:BaiduMobAdLpStyleDefault];//lp颜色配置
    
    float height = kBaiduBannerAdHeight;
    BaiduMobAdView *baiduAdView = [[BaiduMobAdView alloc] init];
    NSDictionary *dict = kBookCityDataDict;
    NSString *serverAdUnitTag = dict[@"ad"][@"search_banner"][@"baidu"][@"AdUnitTag"];
    NSString *adunit = serverAdUnitTag.length>0?serverAdUnitTag:kBaiduMobSearchBannerAdUnitTag;
    baiduAdView.AdUnitTag = adunit;
    baiduAdView.AdType = BaiduMobAdViewTypeBanner;
    _baiduAdView = baiduAdView;
    CGFloat adViewY = MTScreenH - height - SafeAreaTopHeight;
    if (iPhoneX) {
        adViewY = adViewY - 34;
    }
    baiduAdView.frame = CGRectMake(0, adViewY, MTScreenW, height);
    [self.controller.view addSubview:baiduAdView];
    
    baiduAdView.delegate = self;
    [baiduAdView start];
}

- (NSString *)publisherId {
    
    NSDictionary *dict = kBookCityDataDict;
    NSString *serverBaiduadid = dict[@"ad"][@"search_banner"][@"baidu"][@"AppId"];
    
    return serverBaiduadid.length>0?serverBaiduadid:kBaiduMobAdPublisherId;
}

-(BOOL) enableLocation
{
    //启用location会有一次alert提示
    return NO;
}

/**
 *  广告载入失败
 */
- (void)failedDisplayAd:(BaiduMobFailReason)reason{
    MTLog(@"百度条幅广告载入失败:%d",reason);
    NSString *event;
    switch (adlocation) {
        case BannerAdInSearch:
            event = @"BBannerFailedDisplayAd1";
            break;
        default:
            break;
    }
    [[BaiduMobStat defaultStat] logEvent:event eventLabel:@"BBannerFailedDisplayAd1"];
    [self loadNextBannerAd];
}

/**
 *  本次广告展示成功时的回调
 */
- (void)didAdImpressed{
    MTLog(@"百度条幅广告展示成功");
    [self didLoadBannerHeight:kBaiduBannerAdHeight];
    NSString *event;
    switch (adlocation) {
        case BannerAdInSearch:
            event = @"BbannerDidAdImpressed1";
            break;
        default:
            break;
    }
    [[BaiduMobStat defaultStat] logEvent:event eventLabel:@"BbannerDidAdImpressed1"];
}

/**
 *  本次广告展示被用户点击时的回调
 */
- (void)didAdClicked{
    MTLog(@"百度条幅广告被用户点击");
    NSString *event;
    switch (adlocation) {
        case BannerAdInSearch:
            event = @"BbannerDidAdClicked1";
            break;
        default:
            break;
    }
    [[BaiduMobStat defaultStat] logEvent:event eventLabel:@"BbannerDidAdClicked1"];
}

//百度广告关闭的回调
- (void)didAdClose{
    if ([self.delegate respondsToSelector:@selector(bannerAdViewClose:)]) {
        [self.delegate bannerAdViewClose:self];
    }
}

-(void)didLoadBannerHeight:(float)height{
    
    if ([self.delegate respondsToSelector:@selector(bannerAdView:didLoadBannerAdHeight:)]) {
        [self.delegate bannerAdView:self didLoadBannerAdHeight:height];
    }
}
/*
#pragma mark - Google MobAd
-(void)loadGoogleAdView{
    
    GADNativeExpressAdView *nativeExpressAdView = [[GADNativeExpressAdView alloc]initWithAdSize:GADAdSizeFromCGSize(CGSizeMake(MTScreenW, 100))];
    NSString *adunit = nil;
    switch (adlocation) {
        case BannerAdInSearch:
            adunit = kGoogleAdMobSearchNativeBannerUnit;
            break;
        default:
            break;
    }
    nativeExpressAdView.adUnitID = adunit;
    nativeExpressAdView.rootViewController = _controller;
    nativeExpressAdView.delegate = self;
    self.nativeExpressAdView = nativeExpressAdView;
    
    GADRequest *request = [GADRequest request];
    request.testDevices = @[@"09af194dcaa0b5871f3dbb4edf577f80"];
    [self.nativeExpressAdView loadRequest:request];
}

-(void)showGoogleNativeExpressAdView{
    
    CGRect frame = self.nativeExpressAdView.frame;
    frame.origin.y = MTScreenH - kGoogleBannerAdHeight - SafeAreaTopHeight;
    self.nativeExpressAdView.frame = frame;
    [_controller.view addSubview:self.nativeExpressAdView];
    self.nativeExpressAdView.centerX = self.controller.view.centerX;
    
    [self didLoadBannerHeight:kGoogleBannerAdHeight];
}

- (void)nativeExpressAdViewDidReceiveAd:(GADNativeExpressAdView *)nativeExpressAdView{
    
    MTLog(@"google原生（banner）广告加载成功");
    [self showGoogleNativeExpressAdView];
    NSString *event;
    switch (adlocation) {
        case BannerAdInSearch:
            event = @"GoogleBannerAdDidReceive1";
            break;
        default:
            break;
    }
    [[BaiduMobStat defaultStat] logEvent:event eventLabel:nativeExpressAdView.adUnitID];
}

- (void)nativeExpressAdView:(GADNativeExpressAdView *)nativeExpressAdView
didFailToReceiveAdWithError:(GADRequestError *)error{
    MTLog(@"google原生（banner）广告加载失败 error：%@",error);
    [self loadNextBannerAd];
    
    NSString *event;
    switch (adlocation) {
        case BannerAdInSearch:
            event = @"GoogleBannerAdFailToReceiveAd1";
            break;
        default:
            break;
    }
    [[BaiduMobStat defaultStat] logEvent:event eventLabel:nativeExpressAdView.adUnitID];
}

- (void)nativeExpressAdViewWillLeaveApplication:(GADNativeExpressAdView *)nativeExpressAdView{
   
    NSString *event;
    switch (adlocation) {
        case BannerAdInSearch:
            event = @"GoogleBannerLeaveApplication1";
            break;
        default:
            break;
    }
    [[BaiduMobStat defaultStat] logEvent:event eventLabel:nativeExpressAdView.adUnitID];
}

- (void)nativeExpressAdViewDidDismissScreen:(GADNativeExpressAdView *)nativeExpressAdView{
    [self didAdClose];
}
*/
//
//#pragma mark - 巨鲨Banner广告
//-(void)loadJushaBanner{
//    
//    _bottomBannerSDK = [[JushaBannerSDK alloc] initWithUserId:kJushaUserid appId:kJushaAppId appKey:kJushaAppKey];
//    _bottomBannerSDK.delegate = self;
//    [_bottomBannerSDK showBannerInController:_controller position:BannerPositionBottom];
//}
//
//-(void)closeBanner:(JushaBannerSDK *)bannerSDK{
//   
//    [[BaiduMobStat defaultStat] logEvent:@"JushaBannerClose" eventLabel:@"close"];
//    [self didAdClose];
//}
//
//-(void)clickBanner:(JushaBannerSDK *)bannerSDK{
//   
//    [[BaiduMobStat defaultStat] logEvent:@"JushaBannerClick" eventLabel:@"click"];
//}
//
//-(void)successToFetchBanner:(JushaBannerSDK *)bannerSDK height:(CGFloat)height{
//    
//    [[BaiduMobStat defaultStat] logEvent:@"JushaBannerSuccess" eventLabel:@"success"];
//    [self didLoadBannerHeight:height];
//}
//
//-(void)failToFetchBanner:(JushaBannerSDK *)bannerSDK error:(NSError *)error{
//    
//    [[BaiduMobStat defaultStat] logEvent:@"JushaBannerFail" eventLabel:@"fail"];
//    [self loadNextBannerAd];
//}

@end
